package com.xuecheng.learning.service.impl;

import com.xuecheng.base.model.RestResponse;
import com.xuecheng.base.utils.JsonUtil;
import com.xuecheng.content.model.dto.TeachPlanDto;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.feignclient.MediaServiceClient;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.service.LearningService;
import com.xuecheng.learning.service.MyCourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-07-26
 * Time: 11:47
 */

@Slf4j
@Service
public class LearningServiceImpl implements LearningService {

    @Resource
    ContentServiceClient contentServiceClient;

    @Resource
    MyCourseTablesService myCourseTablesService;

    @Resource
    MediaServiceClient mediaServiceClient;


    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {

        //查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish == null) {
            RestResponse.validFail("课程不存在");
        }


        //用户已登录
        if (!StringUtils.isEmpty(userId)) {
            //取学习资格
            XcCourseTablesDto learningStatus = myCourseTablesService.getLearningStatus(userId, courseId);
            //学习资格，[{"code":"702001","desc":"正常学习"},
            // {"code":"702002","desc":"没有选课或选课后没有支付"},
            // {"code":"702003","desc":"已过期需要申请续期或重新支付"}]
            String learnStatus = learningStatus.getLearnStatus();
            switch (learnStatus) {
                case "702001":
                    return mediaServiceClient.getPlayUrlByMediaId(mediaId);
                case "702002":
                    return RestResponse.validFail("无法观看，由于没有选课或选课后没有支付");
                case "702003":
                    return RestResponse.validFail("您的选课已过期需要申请续期或重新支付");
            }
        }
        // 未登录或未选课判断是否收费
        String charge = coursepublish.getCharge();
        if (charge.equals("201000")) {//免费可以正常学习
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }
        // 是否可以试学
        boolean flag = isPreview(coursepublish, mediaId);
        if (flag) {
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }

        return RestResponse.validFail("请购买课程后继续学习");

    }

    private boolean isPreview(CoursePublish coursepublish, String mediaId) {
        String json = coursepublish.getTeachplan();
        List<TeachPlanDto> teachPlanDtos = JsonUtil.jsonToList(json, TeachPlanDto.class);
        List<TeachPlanDto> temp = new ArrayList<>();
        getTeachPlanDtos(teachPlanDtos, temp);
        TeachPlanDto teachPlanDto = test3(temp, mediaId);
        if (teachPlanDto != null) {
            String isPreview = teachPlanDto.getIsPreview();
            return "1".equals(isPreview);
        }
        return false;
    }


    private TeachPlanDto test3(List<TeachPlanDto> temp, String mediaId) {
        if (CollectionUtils.isEmpty(temp)) {
            return null;
        }
        for (TeachPlanDto teachPlanDto : temp) {
            if (teachPlanDto.getTeachplanMedia().getMediaId().equals(mediaId)) {
                return teachPlanDto;
            }
        }
        return null;
    }


    private void getTeachPlanDtos(List<TeachPlanDto> teachPlanDtos, List<TeachPlanDto> temp) {
        if (CollectionUtils.isEmpty(teachPlanDtos)) {
            return;
        }
        teachPlanDtos.forEach(teachPlanDto -> {
            if (teachPlanDto.getTeachplanMedia() != null) {
                temp.add(teachPlanDto);
            }
            if (!CollectionUtils.isEmpty(teachPlanDto.getTeachPlanTreeNodes())) {
                getTeachPlanDtos(teachPlanDto.getTeachPlanTreeNodes(), temp);
            }
        });
    }
}
